#!/bin/bash
#
# vInstaller an Android upgrade tool.
# Copyright (C) 2012 Vincenzo Frascino <vincenzo.frascino@st.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
#

# GNU Parted Binary
#PARTED=parted.static
PARTED=parted

echo Starting installing procedure for 1340AB_LCAD_IRIS...

/mnt/sdcard/vInstaller/lcd/view_script.sh /mnt/sdcard/vInstaller/lcd/vInstaller_start.raw &

sleep 5

echo Erasing and mounting MLC Flash Drive...
# Search for an USB usable device: the first found is used.
for i in {a..z}; do
#for i in {b..z}; do
	# Create the string for the device to test. 
	TEST="sd${i}"
	echo test device: $TEST
	# Test the Device.
	TRYDEV=`dmesg | grep "logical blocks" | grep $TEST`
	# Escape device name in order to be useful with Bash Script.
	ETRYDEV=`printf %q "$TRYDEV"`
	if [ -n "$ETRYDEV" ]
		then
			USEDEV=$TEST
			break
	fi
done

echo use device: $USEDEV

# Clear the MBR and partition table
dd if=/dev/zero of=/dev/$USEDEV bs=512 count=1
${PARTED} -s /dev/$USEDEV mklabel msdos

# Calculate the total Space on the drive.
TOTAL=`${PARTED} -s /dev/$USEDEV unit mb print free | grep Free | awk '{print $3}' | cut -d "M" -f1`

echo total space: $TOTAL

# calculate start points
let BOOT_START=200
let ROOT_START=$BOOT_START+1000
let DATA_START=$ROOT_START+2000
let STOTAL=$TOTAL-2

# partitions IN ORDER
${PARTED} -s /dev/$USEDEV mkpart primary fat32 1 $BOOT_START
${PARTED} -s /dev/$USEDEV mkpart primary ext2 $BOOT_START $ROOT_START
${PARTED} -s /dev/$USEDEV mkpart primary ext2 $ROOT_START $DATA_START
${PARTED} -s /dev/$USEDEV mkpart primary fat32 $DATA_START $STOTAL

sleep 3

# create mounting dirs
mkdir /mnt/boot
mkdir /mnt/root
mkdir /mnt/data
mkdir /mnt/media

# create fs
/root/bin/mkfs.vfat /dev/${USEDEV}1 -n boot
mkfs.ext3 /dev/${USEDEV}2 -L root -m 1
mkfs.ext3 /dev/${USEDEV}3 -L data
/root/bin/mkfs.vfat /dev/${USEDEV}4 -n media

sleep 3

# mount fs
mount -t vfat /dev/${USEDEV}1 /mnt/boot
mount -t ext3 /dev/${USEDEV}2 /mnt/root
mount -t ext3 /dev/${USEDEV}3 /mnt/data
mount -t vfat /dev/${USEDEV}4 /mnt/media

killall /mnt/sdcard/vInstaller/lcd/view_script.sh

/mnt/sdcard/vInstaller/lcd/view_script.sh /mnt/sdcard/vInstaller/lcd/vInstaller_50.raw &

# copying files
echo
echo OK, copying files...
/root/bin/tar xzvf /mnt/sdcard/vInstaller/rootfs/root.tar.gz -C /mnt/root
sync
/root/bin/tar xzvf /mnt/sdcard/vInstaller/rootfs/boot.tar.gz -C /mnt/boot
sync
/root/bin/tar xzvf /mnt/sdcard/vInstaller/rootfs/data.tar.gz -C /mnt/data
sync
/root/bin/tar xzvf /mnt/sdcard/vInstaller/rootfs/media.tar.gz -C /mnt/media
sync

sleep 5

cp /mnt/sdcard/vInstaller/rootfs/init.rc /mnt/root
cp /mnt/sdcard/vInstaller/rootfs/vold.fstab /mnt/root/system/etc

umount /mnt/data
umount /mnt/boot
umount /mnt/root
umount /mnt/media

rm -fr /mnt/data
rm -fr /mnt/boot
rm -fr /mnt/root
rm -fr /mnt/media

sync

killall /mnt/sdcard/vInstaller/lcd/view_script.sh

/mnt/sdcard/vInstaller/lcd/view_script.sh /mnt/sdcard/vInstaller/lcd/vInstaller_end.raw &
